package com.vincent.kdp.ui.fragment.presenter;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.widget.Toast;

import com.vincent.baseapp.base.BasePresenter;
import com.vincent.baseapp.util.SPUtil;
import com.vincent.kdp.http.RequestCallBack;
import com.vincent.kdp.ui.bean.entity.NewCommodity;
import com.vincent.kdp.ui.bean.response.NewCommodityListOutput;
import com.vincent.kdp.ui.fragment.contract.NewContract;
import com.vincent.kdp.ui.fragment.repository.NewRepository;
import com.vincent.kdp.util.RegexUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

import static com.vincent.kdp.ui.Constants.SPKeys.seriesId;

public class NewPresenter extends BasePresenter<NewContract.View> implements NewContract.Presenter {

    private Context context;
    private NewRepository mNewRepository;
    public NewPresenter(Context context){
        this.context=context;
        mNewRepository=new NewRepository();
    }

    @Override
    public void initView(Intent intent) {
        mView.showLoadDialog();
        mNewRepository.reqCommodityTypeList(new RequestCallBack() {
            @Override
            public void onSuccess(Object object) {
                if(object!=null){
                    NewCommodityListOutput output= (NewCommodityListOutput) object;
                    mView.getTypeListSuccess(output);
                    //把第一张图片存下
                    saveFirstImage(output);
                }
            }

            @Override
            public void onFalse(String msg) {
                Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
                if(mView!=null){
                    mView.dismissLoadDialog();
                }
            }
        });
    }

    private void saveFirstImage(NewCommodityListOutput output) {
        //每个系列的第一
        List newFirstImageList=new ArrayList<String>();
        //系列的ID
        List newIdList=new ArrayList<String>();
        //系列名称
        List newNameList=new ArrayList<String>();

        List<NewCommodity> data = output.getData();
        for(NewCommodity newCommodity:data){
            String image=RegexUtils.trimNull(newCommodity.getImage());
            if(!image.isEmpty()){
                List imageList=Arrays.asList(image.split(","));
                if(imageList!=null&&imageList.size()>0){

                    newFirstImageList.add(imageList.get(0));
                    newIdList.add(newCommodity.getSeriesId());
                    newNameList.add(newCommodity.getSeriesName());
                }
            }
        }
        SPUtil.remove("newFirstImageList");
        String listString=TextUtils.join(",",newFirstImageList);
        SPUtil.put("newFirstImageList",listString);
        //传系列id
        SPUtil.remove("newIdList");
        String idString=TextUtils.join(",",newIdList);
        SPUtil.put("newIdList",idString);
        //传系列名字
        SPUtil.remove("newNameList");
        String nameString=TextUtils.join(",",newNameList);
        SPUtil.put("newNameList",nameString);

    }
}
